﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcWebShose.BUS;
using System.Data;
using MvcWebShose.Models;

namespace MvcWebShose.Controllers
{
    public class SanPhamController : Controller
    {
        //
        // GET: /SanPham/

        public ActionResult Index()
        {
            return View();
        }
        //
        public ActionResult ShowAllSanPham()
        {
            List<SanPhamModels> lstSanPham = SanPhamBUS.ListSanPham();
            return View(lstSanPham);
        }
        //
        // GET: /SanPham/Details/5

        public ActionResult Details(int MA_SP)
        {
            SanPhamModels item =SanPhamBUS.getDetailBySanPhamID(MA_SP);
            return View(item);
        }

        //
        // GET: /SanPham/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /SanPham/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /SanPham/Edit/5
 
        public ActionResult Edit(int id)
        {
            var item = SanPhamBUS.getSanPhamDetails(id);
            return View(item);
        }

        //
        // POST: /SanPham/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                SanPhamModels sanp = SanPhamBUS.getSanPhamDetails(id);
                sanp.MA_LOAI = int.Parse(collection.Get("MA_LOAI"));
                sanp.MA_NCC = int.Parse(collection.Get("MA_NCC"));
                sanp.GIA_SP = float.Parse(collection.Get("GIA_SP"));
                sanp.MOTA= collection.Get("MOTA");
                sanp.TEN_SP = collection.Get("TEN_SP");
                sanp.QUYCACH = collection.Get("QUYCACH");
                sanp.TRANGTHAI =int.Parse( collection.Get("TRANGTHAI"));
                sanp.HINHANH = collection.Get("HINHANH");
          
               SanPhamBUS.EditSanPham(sanp);
                return RedirectToAction("ShowAllSanPham");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SanPham/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /SanPham/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
